package com.hhf.study.config;

import com.hhf.study.listener.TestChatModelListener;
import com.hhf.study.service.ChatAssistant;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.List;

/**
 * @author Daniel-黄海峰
 * @Description
 * @create 2025/8/2 21:55
 */
@Configuration
public class ChatLanguageModelConfig
{
    @Bean
    public ChatLanguageModel chatLanguageModel()
    {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("LANGCHAIN4J_KEY"))
                .modelName("qwen-turbo-0624")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true) // 日志界别设置为debug才有效
                .logResponses(true)// 日志界别设置为debug才有效
                .listeners(List.of(new TestChatModelListener())) //监听器
                .maxRetries(1)// 重试机制
                .timeout(Duration.ofSeconds(10)) // 设置10秒超时
                .build();
    }

    @Bean
    public ChatAssistant chatAssistant(ChatLanguageModel chatLanguageModel)
    {
        return AiServices.create(ChatAssistant.class, chatLanguageModel);
    }
}